package org.zhixiang.gisQuery.query;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.zhixiang.gisQuery.rtree.Rectangle;
import org.zhixiang.gisQuery.utils.JacksonUtil;

/**
 * 
 * @say little Boy, don't be sad.
 * @name Rezar
 * @time 2018年9月25日 上午11:29:56
 * @Desc 些年若许,不负芳华.
 *
 */
public class MbrConfigHelper {

	public static void main(String[] args) throws IOException {
		doConfig("/Users/rezar/Desktop/gisDatas");
		System.out.println(RectangleMBR.countOfMulti);
	}

	public static void doConfig(String gisDataDirStr) throws IOException {
		File gisDataDir = new File(gisDataDirStr);
		if (!gisDataDir.exists() || !gisDataDir.isDirectory()) {
			System.out.println("init failure , " + gisDataDirStr + " not exists or not a dir");
			return;
		}
		File[] listFiles = gisDataDir.listFiles(new FilenameFilter() {
			@Override
			public boolean accept(File dir, String name) {
				return name.endsWith(GPSLocator.GIS_ENDFIX);
			}
		});
		for (File file : listFiles) {
			String readFileToString = FileUtils.readFileToString(file);
			List<RectangleMBR> parseDis = JacksonUtil.str2List(readFileToString, RectangleMBR.class); // GaoDeJsonDataParser.parseJsonDis2();
			for (RectangleMBR mbr : parseDis) {
				configMbr(mbr);
			}
			FileUtils.write(file, JacksonUtil.obj2Str(parseDis));
			System.out.println(file.getAbsolutePath() + " config over");
		}
	}

	protected static void configMbr(RectangleMBR mbr) {
		List<SelfPoint> polyline = mbr.getPolyline();
		double minX = Double.MAX_VALUE;
		double maxX = Double.MIN_VALUE;
		double minY = Double.MAX_VALUE;
		double maxY = Double.MIN_VALUE;
		for (SelfPoint selfPoint : polyline) {
			if (selfPoint.x < minX) {
				minX = selfPoint.x;
			}
			if (selfPoint.x > maxX) {
				maxX = selfPoint.x;
			}
			if (selfPoint.y < minY) {
				minY = selfPoint.y;
			}
			if (selfPoint.y > maxY) {
				maxY = selfPoint.y;
			}
		}
		Rectangle re = new Rectangle(minX, minY, maxX, maxY);
		mbr.setRectangle(re);
		mbr.initMinBoundes(200, 200);
	}

}
